package com.resou.reader.signin;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.data.signin.model.SignInInfo;
import com.resou.reader.data.signin.model.SignInInfo2;
import com.resou.reader.data.signin.model.UserWeekSignBean;
import com.resou.reader.utils.DeviceDimensionsHelper;
import com.resou.reader.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WeekAdapter extends RecyclerView.Adapter<WeekAdapter.WeekHolder> {
    private static final String TAG = "WeekAdapter-App";
    private List<UserWeekSignBean> mWeekSignBeanList;
    private Context mContext;
    private final int mDisplayWidth;
    private final float mPixel;
    private SignInInfo2 mSignInInfo2;

    public WeekAdapter(Context context) {
        mContext = context;
        mWeekSignBeanList = new ArrayList<>();
        mDisplayWidth = DeviceDimensionsHelper.getDisplayWidth(context);
        mPixel = DeviceDimensionsHelper.convertDpToPixel(2, context);
    }

    private OnSignInInteractionListener mInteractionListener;

    public void setInteractionListener(OnSignInInteractionListener interactionListener) {
        mInteractionListener = interactionListener;
    }

    @NonNull
    @Override
    public WeekHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_item_signin_item, viewGroup, false);
        view.getLayoutParams().width = (int) (mDisplayWidth / 7 - mPixel);
        return new WeekHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WeekHolder holder, int i) {
        UserWeekSignBean signBean = mWeekSignBeanList.get(i);
        int weekday = signBean.getWeekday();
        if (weekday == 1) {
            holder.tvWeek.setText("一");
            configureWeekHolder(holder, signBean, i);
        }
        if (weekday == 2) {
            holder.tvWeek.setText("二");
            configureWeekHolder(holder, signBean, i);
        }
        if (weekday == 3) {
            holder.tvWeek.setText("三");
            configureWeekHolder(holder, signBean, i);
        }
        if (weekday == 4) {
            holder.tvWeek.setText("四");
            configureWeekHolder(holder, signBean, i);
        }
        if (weekday == 5) {
            holder.tvWeek.setText("五");
            configureWeekHolder(holder, signBean, i);
        }
        if (weekday == 6) {
            holder.tvWeek.setText("六");
            configureWeekHolder(holder, signBean, i);
        }
        if (weekday == 7) {
            holder.tvWeek.setText("七");
            configureWeekHolder(holder, signBean, i);
        }
    }

    private void configureWeekHolder(WeekHolder holder, final UserWeekSignBean signBean, final int i) {
        int signStatus = signBean.getSignStatus();
        switch (signStatus) {
            case 1:
                //1：待补签
                holder.tvCheckInStatus.setTextColor(mContext.getResources().getColor(R.color.text_black));
                holder.tvCheckInStatus.setText("补");
                holder.tvCheckInStatus.setActivated(false);
                holder.ivTrue.setVisibility(View.GONE);
                holder.tvCheckInStatus.setOnClickListener(v -> {
                    //判断用户当天补签次数
                    if (mSignInInfo2.getUserDailySuppCount() == 0) {
                        //没补签过
                        //分享
                        mInteractionListener.showShareDialog(signBean.getWeekday());
                    } else {
                        //充值
                        mInteractionListener.showRechargeDialog(signBean.getWeekday());
                    }
//                        补签按钮点击后分享成功后才能签到
//                    ToastUtil.makeShortToast("您当天补签" + mSignInInfo2.getUserDailySuppCount() + "次");
                });
                break;
            case 2:
                //2：已签到
                holder.tvCheckInStatus.setText("+" + signBean.getVoucher());
                holder.tvCheckInStatus.setActivated(true);
                holder.ivTrue.setVisibility(View.VISIBLE);
                holder.tvCheckInStatus.setOnClickListener(v -> ToastUtil.makeShortToast("您已签到"));
                break;
            case 3:
                //3：可签到
                holder.tvCheckInStatus.setText("签");
                holder.tvCheckInStatus.setActivated(false);
                holder.ivTrue.setVisibility(View.GONE);
                holder.tvCheckInStatus.setOnClickListener(v -> mInteractionListener.OnSignInClicked(0));
                break;
            default:
                //4：不可签到。
                holder.tvCheckInStatus.setText("签");
                holder.tvCheckInStatus.setActivated(false);
                holder.ivTrue.setVisibility(View.GONE);
//                holder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
////                        mInteractionListener.showRechargeDialog(signBean.getWeekday());
////                        ToastUtil.makeShortToast(signBean.getWeekdayStr() + "才能再签到哦！");
//                    }
//                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mWeekSignBeanList.size();
    }

    public void setData(List<UserWeekSignBean> userWeekSign) {
        mWeekSignBeanList = userWeekSign;
        notifyDataSetChanged();
    }

    public void setData(SignInInfo2 data) {
        mSignInInfo2 = data;
        notifyDataSetChanged();
    }

    public void setData(SignInInfo data) {
        notifyDataSetChanged();
    }

    class WeekHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_week)
        TextView tvWeek;
        @BindView(R.id.tv_check_in_status)
        TextView tvCheckInStatus;
        @BindView(R.id.iv_true)
        ImageView ivTrue;

        public WeekHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnSignInInteractionListener {

        void OnSignInClicked(int weekday);

        void showShareDialog(int weekday);

        void showRechargeDialog(int weekday);
    }
}
